/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package demo;

import java.io.IOException;
import java.io.PrintWriter;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Van Hung
 */
public class Controller extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        HttpSession session = request.getSession();
        String task = request.getParameter("task");

        if(task.equals("login")){
            String user = request.getParameter("user");
            String pass = request.getParameter("pass");

            try {
                InitialContext context = new InitialContext();
                AccountLocalHome home = (AccountLocalHome)context.lookup("AccountLocalJNDI");
                if(home.checkLogin(user, pass)){
                    session.setAttribute("ACC",home.findByAll());
                    response.sendRedirect("View.jsp");
                }
                else{
                    response.sendRedirect("Error.jsp");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if(task.equals("insert")){
            String user = request.getParameter("user");
            String pass = request.getParameter("pass");
            String role = request.getParameter("role");

             try {
                 InitialContext context = new InitialContext();
                 AccountLocalHome home = (AccountLocalHome) context.lookup("AccountLocalJNDI");
                 home.create(user, pass, role);
                 session.setAttribute("ACC", home.findByAll());
                 response.sendRedirect("View.jsp");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if(task.equals("delete")){
            String user = request.getParameter("user");

             try {
                 InitialContext context = new InitialContext();
                 AccountLocalHome home = (AccountLocalHome) context.lookup("AccountLocalJNDI");
                 AccountLocal local = home.findByPrimaryKey(user);
                 local.remove();
                 
                 session.setAttribute("ACC", home.findByAll());
                 response.sendRedirect("View.jsp");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (task.equals("update")) {
            String user = request.getParameter("user");
            String pass = request.getParameter("pass");
            String role = request.getParameter("role");
            try {
                InitialContext context = new InitialContext();
                AccountLocalHome home = (AccountLocalHome) context.lookup("AccountLocalJNDI");
                AccountLocal local = home.findByPrimaryKey(user);
                local.setPassword(pass);
                local.setRole(role);
                session.setAttribute("ACC", home.findByAll());
                response.sendRedirect("View.jsp");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
         if (task.equals("search")) {
            String user =request.getParameter("user");
            try {
                InitialContext context = new InitialContext();
                AccountLocalHome home = (AccountLocalHome) context.lookup("AccountLocalJNDI");
                session.setAttribute("ACC", home.findByUsername("%"+user+"%"));
                response.sendRedirect("View.jsp");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
